package com.springboot.service.impl;

import com.springboot.dto.MessageView;
import com.springboot.mapper.*;
import com.springboot.model.*;
import com.springboot.service.PatientMyInformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * 患者端我的通知业务
 */
@Service
public class PatientMyInformServiceImpl implements PatientMyInformService {
    @Autowired
    MessageInformMapper messageInformMapper;

    @Autowired
    AssistantListFormMapper assistantListFormMapper;

    @Autowired
    AssistantMessageInformMapper assistantMessageInformMapper;

    @Autowired
    DoctorInfoFormMapper doctorInfoFormMapper;

    @Autowired
    DoctorFieldFormMapper doctorFieldFormMapper;

    @Autowired
    UserLoginFormMapper userLoginFormMapper;

    @Autowired
    OrderFormMapper orderFormMapper;

    /**
     * @ description:根据患者ID查找我的通知，展示今天的或者未读的通知
     * @ param     :[patientId]
     * @ return    :com.springboot.model.MessageInform
     * @ date       :2020/12/22

    */
    @Override
    public ArrayList<MessageView> showMessageByPatientId(Integer patientId) {
        ArrayList<MessageInform> messageInforms = new ArrayList<>();
        try {
            messageInforms = messageInformMapper.selectByPatientId(patientId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientMyInformServiceImpl 错误");
            System.out.println("根据患者ID查找通知 错误");
        }
        ArrayList<MessageView> messageViews = new ArrayList<>();
        for(int i=0;i<messageInforms.size();i++)
        {
            MessageInform messageInform = messageInforms.get(i);
            MessageView messageView = new MessageView();
            try {
                Integer doctorId = messageInform.getDoctorId();
                DoctorInfoForm doctorInfoForm = doctorInfoFormMapper.selectByPrimaryKey(doctorId);
                DoctorFieldForm doctorFieldForm = doctorFieldFormMapper.selectDoctorFieldById(doctorId);
                UserLoginForm userLoginForm = userLoginFormMapper.selectByPrimaryKey(patientId);
                messageView.setMessageInform(messageInform);
                messageView.setDoctorInfoForm(doctorInfoForm);
                messageView.setDoctorFieldForm(doctorFieldForm);
                messageView.setPatientName(userLoginForm.getUserName());
                SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                messageView.setMessageDateTime(sdf.format(messageInform.getMessageDatetime()));
            }
            catch (Exception e )
            {
                e.printStackTrace();
                System.out.println("PatientMyInformServiceImpl 错误");
                System.out.println("拼接消息视图 错误");
            }
            messageViews.add(messageView);
        }
        return messageViews;
    }

    /**
     * @ description:根据消息编号把患者消息状态改为已读
     * @ param     :[messageId]
     * @ return    :int
     * @ date       :2020/12/22

    */
    @Override
    public int updateMessageByMessageId(Integer messageId) {
        MessageInform messageInform = messageInformMapper.selectByPrimaryKey(messageId);
        messageInform.setMessageState1("已读");
        if(messageInformMapper.updateByPrimaryKey(messageInform)==1)
        {
            return 1;
        }
        else {
            return 0;
        }
    }

    /**
     * @ description  :预约时添加消息或者取消预约时添加消息（患者和医生）,返回消息ID
     * @ param        :[messageInform]
     * @ return       :messageId
     * @ date         :2020/12/22

    */
    @Override
    public int insertMessage(MessageInform messageInform) {
        int result=0;
        try {
            result = messageInformMapper.insertSelective(messageInform);
            try {
                //messageInform = messageInformMapper.selectMessageId(messageInform);
                result = messageInformMapper.selectMessageId2();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.out.println("PatientMyInformServiceImpl 错误");
                System.out.println("messageInformMapper.selectMessageId(messageInform) 错误");
            }
        }
        catch (Exception e1)
        {
            result=-1;
            e1.printStackTrace();
            System.out.println("PatientMyInformServiceImpl 错误");
            System.out.println("messageInformMapper.insertSelective(messageInform) 错误");
        }
        finally {
            System.out.println("返回的message: "+result);
            return result;
        }
    }


    /**
     * @ description:根据医生ID查找助理,把消息插入助理消息表
     * @ param      :[doctorId, messageId]
     * @ return     :int
     * @ date       :2020/12/22

    */
    @Override
    public int insertAssistantMessageInformByDoctorId(Integer doctorId,Integer messageId) {
        int result = 1;
        ArrayList<AssistantListFormKey> assistantListFormKeys = new ArrayList<>();
        try {
            assistantListFormKeys = assistantListFormMapper.selectByDoctorId(doctorId);
        }
        catch (Exception e)
        {
            result=0;
            e.printStackTrace();
            System.out.println("PatientMyInformServiceImpl 出错");
            System.out.println("assistantListFormMapper.selectByDoctorId(doctorId) 出错");
        }
        //添加助理通知
        for(int i=0;i<assistantListFormKeys.size();i++)
        {
            AssistantListFormKey assistantListFormKey = assistantListFormKeys.get(i);
            AssistantMessageInform assistantMessageInform = new AssistantMessageInform();
            assistantMessageInform.setMessageId(messageId);
            assistantMessageInform.setAssistantId(assistantListFormKey.getAssistantId());
            try {
                assistantMessageInformMapper.insert(assistantMessageInform);
            }
            catch (Exception e)
            {
                result=0;
                System.out.println("PatientMyInformServiceImpl 出错");
                System.out.println("assistantMessageInformMapper.insert(assistantMessageInform) 出错");
            }
        }
        return result;
    }

    /**
     * @ description:根据预约Id返回预约信息表
     * @ param      :[bookId]
     * @ return     :com.springboot.model.OrderForm
     * @ date       :2020/12/23

    */
    @Override
    public OrderForm findOrderFormByBookId(Integer bookId) {
        OrderForm orderForm = new OrderForm();
        try {
            orderForm = orderFormMapper.selectByPrimaryKey(bookId);
        }
        catch (Exception e)
        {
            System.out.println("PatientMyInformServiceImpl 出错");
            System.out.println("orderFormMapper.selectByPrimaryKey(bookId) 出错");
        }
        finally {
            return orderForm;
        }
    }

    @Override
    public int readAllMessage(Integer patientId) {
        int a=0;
        try {
            a = messageInformMapper.updateAllByPatientId(patientId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("患者一键已读出错");
        }
        return  a;
    }

    /**
     * @ description:患者在我的页面展示未读消息数量
     * @ param      :[patientId]
     * @ return     :int
     * @ date       :2020/12/31

    */
    @Override
    public int findUnreadNum(Integer patientId) {
        int num=0;
        try {
            num = messageInformMapper.selectUnreadNumByPatientId(patientId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("PatientMyInformServiceImpl 查找未读数量出错");
        }
        return num;
    }
}
